<%@ page pageEncoding="UTF-8"%>
<%@ include file="/common/token.jsp"%>
<%
	
	String date = input.getDesc("date", "");
	Integer state = input.getInt("state", STATE_ADD);
	List<Order> list = new ArrayList<Order>();
	String content = "";
	if (STATE_ADD.equals(state)) {
		content = "CONCAT(FROM_UNIXTIME(registerTime / 1000,'%Y-%m-%d'),'')";	
	} else {
		content = "CONCAT(FROM_UNIXTIME(execTime / 1000,'%Y-%m-%d'),'')";	
	} 
	List<String> min = QBuilder.db("basedb").select("min("+ content +")").from("Order").where(new Where("hospital=", hospital).and("status=", STATUS_ENABLE).and("state=", state)).query().getResultObj();
	if (isEmpty(min)) {
		msg = new Message(30041, "没有数据");
		msg.put("data", new HashMap());
		Tools.encryptPrint(out, msg);
		return;
	}
	if ("".equals(date)) {
		List<String> max = QBuilder.db("basedb").select("max("+content+")").from("Order").where(new Where("hospital=", hospital).and("status=", STATUS_ENABLE).and("state=", state)).query().getResultObj();
		if (isEmpty(max)) {
			msg = new Message(30041, "没有数据");
			msg.put("data", new HashMap());
			Tools.encryptPrint(out, msg);
			return;
		}
		date = max.get(0);
	}
	int count = 1;
	String endDate = before(date, - (count++) * 10);
	while(isEmpty(list)) {
		if (STATE_ADD.equals(state)) {
			list = QBuilder.db("basedb").select().from("Order").where(new Where("hospital =", hospital).and("status = ", STATUS_ENABLE).and("state=", state).and("FROM_UNIXTIME(registerTime / 1000,'%Y-%m-%d') <=", date).and("FROM_UNIXTIME(registerTime / 1000,'%Y-%m-%d')>", endDate)).order("registerTime", QBuilder.desc).query().getResultObj();
		} else {
			list = QBuilder.db("basedb").select().from("Order").where(new Where("hospital =", hospital).and("status = ", STATUS_ENABLE).and("state=", state).and("FROM_UNIXTIME(execTime / 1000,'%Y-%m-%d') <=", date).and("FROM_UNIXTIME(execTime / 1000,'%Y-%m-%d')>", endDate)).order("execTime", QBuilder.desc).query().getResultObj();
		}
		Date _endDate = DateUtil.parseDate(endDate);
		Date minDate = DateUtil.parseDate(min.get(0));
		if (_endDate.before(minDate) || !isEmpty(list)) {
			break;
		}
		endDate = before(date, - (count++) * 10);
	}
	if (isEmpty(list)) {
		msg = new Message(30041, "没有数据");
		msg.put("data", new HashMap());
		Tools.encryptPrint(out, msg);
		return;
	}
	 List<Map> arranges = new ArrayList<Map>();
    for (int i = 0; i < list.size(); i++) {
        Order order = list.get(i);
        state = order.getState();
        Map map = new HashMap();
        map.put("patient_name", order.getUserName());
        map.put("doctor_name", order.getDoctorName());
        map.put("doctor_uid", Tools.getDesc(order.getDoctor() + "", ""));
        map.put("type", Tools.getDesc(order.getType() + "", ""));
        map.put("title", order.getTitle());
        map.put("state", state);
        map.put("register_time", DateUtil.formatDate(new Date(order.getRegisterTime())));
        map.put("exec_time", isEmpty(order.getExecTime()) ? "" : DateUtil.formatDate(new Date(order.getExecTime())));
        map.put("half", order.getHalf());
        map.put("age", order.getAge());
        map.put("phone", order.getPhone());
        map.put("start_time", order.getStartTime());
        map.put("end_time", order.getEndTime());
        map.put("order_id", order.getId());
        arranges.add(map);
    }
	Map map = new HashMap();
	map.put("date", endDate);
	map.put("result", arranges);
    msg.put("data", map);
	Tools.encryptPrint(out, msg);
%>